package cn.kinoko.service.manga.impl;

import cn.hutool.core.util.StrUtil;
import cn.kinoko.common.base.model.BaseServiceImpl;
import cn.kinoko.common.code.ErrorCodeEnum;
import cn.kinoko.model.manga.constant.EpMarkStatus;
import cn.kinoko.common.utils.ExceptionUtil;
import cn.kinoko.mapper.manga.MangaEpRawMapper;
import cn.kinoko.model.manga.entity.MangaEpRaw;
import cn.kinoko.service.manga.MangaEpRawService;
import cn.kinoko.service.manga.MangaMarkService;
import cn.kinoko.service.minio.MinioService;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * @author kinoko
 */
@Service
public class MangaEpRawServiceImpl extends BaseServiceImpl<MangaEpRawMapper, MangaEpRaw> implements MangaEpRawService {

    @Resource
    private MinioService minioService;
    @Resource
    private MangaMarkService mangaMarkService;

    /**
     * 根据章节id删除所有页码
     *
     * @param epId 章节id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delByEpId(Long epId) {
        // 删除标注
        mangaMarkService.delByEpId(epId);
        // 删除页码
        delById(MangaEpRaw::getEpId, epId);
    }

    /**
     * 根据漫画id删除所有页码
     * @param mangaId 漫画id
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delByMangaId(Long mangaId) {
        // 删除标注
        mangaMarkService.delByMangaId(mangaId);
        // 删除页码
        delById(MangaEpRaw::getMangaId, mangaId);
    }

    /**
     * 根据页码id删除
     * @param pageId 页码id
     * @return 执行结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delById(Long pageId) {
        MangaEpRaw page = getById(pageId);
        ExceptionUtil.isNull(page, ErrorCodeEnum.SYSTEM_ERROR_B0405);
        // 删除漫画翻译
        mangaMarkService.delByRid(pageId);
        // 删除页码图片引用
        minioService.deleteFileReference(Collections.singletonList(page.getImageUrl()));
        return true;
    }

    /**
     * 检查是否所有页码ocr翻译完毕
     *
     * @param epId 章节id
     * @return true - 翻译完毕，false - 翻译未完毕
     */
    @Override
    public boolean checkAllTranslate(Long epId) {
        return !lambdaQuery().eq(MangaEpRaw::getEpId, epId).ne(MangaEpRaw::getMarkStatus, EpMarkStatus.DONE.getCode()).exists();
    }

    /**
     * 根据id删除所有页码
     * @param column 业务字段
     * @param id     业务id
     */
    private void delById(SFunction<MangaEpRaw, ?> column, Long id) {
        List<MangaEpRaw> epRaws = lambdaQuery().eq(column, id).list();
        if (!epRaws.isEmpty()) {
            // 删除漫画页码
            lambdaUpdate().eq(column, id).remove();
            // 取消minio文件的引用
            minioService.deleteFileReference(epRaws.stream().map(MangaEpRaw::getImageUrl).toList());
            // 取消空白图的引用
            List<String> blankUrlList = epRaws.stream().map(MangaEpRaw::getBlankUrl).filter(StrUtil::isNotBlank).toList();
            if (!blankUrlList.isEmpty()) {
                minioService.deleteFileReference(blankUrlList);
            }
        }
    }
}
